//验证
$(function(){
	
	jQuery.validator.addMethod("chinese", function(value, element) {
		var chinese = /^[·\u4e00-\u9fa5]+$/;
		return this.optional(element) || (chinese.test(value));
		}, "姓名只能输入中文");
	
	jQuery.validator.addMethod("word", function(value, element) {
		var word = /^[A-Za-z0-9]{4,15}$/;
		return this.optional(element) || (word.test(value));
		}, "4-15位英文或数字");
	
	jQuery.validator.addMethod("mobile", function(value, element) { 
		var length = value.length; 
		var mobile = /^1[0,1,2,3,4,5,6,7,8,9]\d{9}$/ ;
		return this.optional(element) || (length == 11 && mobile.test(value)); 
		}, "手机号码错误");

	jQuery.validator.addMethod("isIdCardNo", function (value, element) {
        return this.optional(element) || isIdCardNo(value);
    }, "错误身份证号码");

	jQuery.validator.addMethod("isMoney", function(value, element) {
		var isMoney = /^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/;
		return this.optional(element) || (isMoney.test(value));
		}, "请输入正确金额");
	
	//卡号
	jQuery.validator.addMethod("bankNum", function(value, element) {
		var bankNum = /^[0-9]*[1-9][0-9]*$/;
		return this.optional(element) || (bankNum.test(value));
		}, "卡号错误");
	
	jQuery.validator.addMethod("oldAge", function(value, element) {
		var oldAge = /^([1-9]\d|\d)$/;
		return this.optional(element) || (oldAge.test(value));
		}, "年龄有误");
	
	jQuery.validator.addMethod("telPhone", function(value, element) {
		var telPhone = /^(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/;
		return this.optional(element) || (telPhone.test(value));
		}, "电话号码有误");
	
	jQuery.validator.addMethod("allMoney", function(value, element) {
		var allMoney = /^\d{1,9}(\.\d{1,2})?$/;
		return this.optional(element) || (allMoney.test(value));
		}, "金额太大或有误，最多2位小数");
	
	jQuery.validator.addMethod("buyMoney", function(value, element) {
		var buyMoney = /^\d{1,3}(\.\d{1,5})?$/;
		return this.optional(element) || (buyMoney.test(value));
		}, "金额太大有误，最多5位小数");
	
	jQuery.validator.addMethod("floatmoney", function(value, element) {
		var floatmoney = /^\d{1,3}(\.\d{1,4})?$/;
		return this.optional(element) || (floatmoney.test(value));
		}, "最多4位小数");
	
	jQuery.validator.addMethod("youzCode", function(value, element) {
		var youzCode = /^[0-9]\d{5}$/;
		return this.optional(element) || (youzCode.test(value));
		}, "邮政编码6位数字");
	
	/*正整数*/
	jQuery.validator.addMethod("zhengZS", function(value, element) {
		var zhengZS = /^[1-9]\d*$/;
		return this.optional(element) || (zhengZS.test(value));
		}, "请输入一个正整数");
	
	jQuery.validator.addMethod("Email", function(value, element) {
		var Email = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		return this.optional(element) || (Email.test(value));
		}, "邮箱地址错误");
	
	jQuery.validator.addMethod("ziMu", function(value, element) {
		var ziMu = /^[A-Za-z ]*$/;
		return this.optional(element) || (ziMu.test(value));
		}, "只能英文或字母");
	
});

function isIdCardNo(num) {
    var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
    var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
    var varArray = new Array();
    var intValue;
    var lngProduct = 0;
    var intCheckDigit;
    var intStrLen = num.length;
    var idNumber = num;
    // initialize
    if ((intStrLen != 15) && (intStrLen != 18)) {
        return false;
    }
    // check and set value
    for (i = 0; i < intStrLen; i++) {
        varArray[i] = idNumber.charAt(i);
        if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
            return false;
        } else if (i < 17) {
            varArray[i] = varArray[i] * factorArr[i];
        }
    }
    if (intStrLen == 18) {
        //check date
        var date8 = idNumber.substring(6, 14);
        if (isDate8(date8) == false) {
            return false;
        }
        // calculate the sum of the products
        for (i = 0; i < 17; i++) {
            lngProduct = lngProduct + varArray[i];
        }
        // calculate the check digit
        intCheckDigit = parityBit[lngProduct % 11];
        // check last digit
        if (varArray[17] != intCheckDigit) {
            return false;
        }
    }
    else {        //length is 15
        //check date
        var date6 = idNumber.substring(6, 12);
        if (isDate6(date6) == false) {
            return false;
        }
    }
    return true;
}

function isDate6(sDate) {
    if (!/^[0-9]{6}$/.test(sDate)) {
        return false;
    }
    var year, month, day;
    year = sDate.substring(0, 4);
    month = sDate.substring(4, 6);
    if (year < 1700 || year > 2500) return false
    if (month < 1 || month > 12) return false
    return true
}

function isDate8(sDate) {
    if (!/^[0-9]{8}$/.test(sDate)) {
        return false;
    }
    var year, month, day;
    year = sDate.substring(0, 4);
    month = sDate.substring(4, 6);
    day = sDate.substring(6, 8);
    var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if (year < 1700 || year > 2500) return false
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
    if (month < 1 || month > 12) return false
    if (day < 1 || day > iaMonthDays[month - 1]) return false
    return true
}